package com.xiaobawang.xjsyy_idea_plugin.panel.config.view;

import com.dtflys.forest.Forest;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.xiaobawang.xjsyy_idea_plugin.forest.ForestApi;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

public class PreConfigDialog extends DialogWrapper {
    private JTextField myTokenField;
    ComboBox<String> orgBox;

    private static final Logger logger = LoggerFactory.getLogger(PreConfigDialog.class);

    public PreConfigDialog() {
        super(true); // use current window as parent
        setTitle("插件配置");
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel myPanel = new JPanel(new GridLayout(2, 1));
        myTokenField = new JTextField("请输入云效token");

        orgBox = new ComboBox<>();
        for (Map.Entry<String, String> entry : GlobalConfig.org2Id.entrySet()) {
            orgBox.addItem(entry.getKey());
            if (entry.getKey().equals("研发中心")) {
                orgBox.setSelectedItem(entry.getKey());
            }
        }

        // 添加一个动作监听器，以打印用户选择的选项
        orgBox.addActionListener(e -> {
            String selected = (String) orgBox.getSelectedItem();
            GlobalConfig.curOrgId = GlobalConfig.org2Id.get(selected);
        });

        myPanel.add(myTokenField);
        myPanel.add(orgBox);
        return myPanel;
    }

    @Override
    protected void doOKAction() {
        String token = myTokenField.getText();
        if (!token.isEmpty()) {
            // token 有效
            GlobalConfig.token = token;
            super.doOKAction();
        } else {
            // token 无效
            Messages.showErrorDialog("输入的Token不合法，请重新输入。", "错误");
        }
    }
}
